<template>
  <div>
    <t-space direction="vertical" style="width: 100%">
      <t-input size="large" placeholder="请输入用户名，不低于6位字符" v-model="data.username"/>
      <t-input size="large" placeholder="请输入登录密码" type="password" v-model="data.password"/>
      <t-button block size="large" @click="OnLogin" :disabled="data.username === '' || data.password === ''">登录账号
      </t-button>
    </t-space>
  </div>
</template>
<script setup lang="ts">
import {onMounted, reactive} from "vue";
import {LoadingPlugin} from "tdesign-vue-next";
import api from "../../../api/api";
import {useRouter} from "vue-router";
import {getDefaultStore} from "../../../store/modules/default";
import NotificationPlugin from "tdesign-vue-next/es/notification/plugin";

const router = useRouter()
const defaultStore = getDefaultStore()
const data = reactive({
  username: '',
  password: ''
})

const OnLogin = async () => {
  LoadingPlugin(true)
  let res = await api.User.Login({
    user: data.username,
    pwd: data.password
  })
  LoadingPlugin(false)
  if (res != null) {
    defaultStore.updateToken(res.data)
    await NotificationPlugin.success({
      title: '成功',
      content: '登录成功'
    })
    setTimeout(() => {
      router.push({
        path: '/admin/my/info'
      })
    }, 1000)
  }
}

onMounted(() => {
  data.username = window.sessionStorage.getItem('username') || ''
})
</script>